/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.khk.lesson;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Ksilem
 */
@ManagedBean
@SessionScoped
public class UserManager {

    private String email;
    private String password;
    private Klant current;

    public String login() {
        KlantController klantController = new KlantController();
        current = klantController.getKlantByLogin(email, password);
        FacesContext context = FacesContext.getCurrentInstance();
        context.getExternalContext().getSessionMap().put("user", current);
        if (current == null) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage("Unknown login, try again"));
            return null;
        } else {
            return "index";
        }
    }

    public Klant getCurrent() {
        return current;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isLoggedIn() {
        return current != null;
    }
    
    public boolean isLoggedOut() {
        return current == null;
    }

    public String logout() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "index?faces-redirect=true";
    }
}
